/**
 * 1. userregister: 用于提交用户注册的表单
 * 2. associationRegister： 用于提交组织注册的表单
 * 3. like : 用于点赞按钮的实现
 */




function userregister() {
    var username = $('#inputUsername').val();
    var password = $('#inputPassword').val();
    var nickname = $('#inputNickname').val();
    var number = $('#inputNumber').val();
    var jwxtpassword = $('#inputJwxtpassword').val();
    var user = {
        username: username,
        password: password,
        nickname: nickname,
        number: number,
        jwxtpassword: jwxtpassword,
    };
    $.ajax({
        type: 'POST',
        contentType: "application/json",
        url: "/register/type=1",
        data: JSON.stringify(user),
        dataType: 'JSON',
        success: function (res) {
            if (res.status) {
                $('#msg').text(res.message + "将在3s之后跳转到登陆页面");
                $('#msg').addClass("text-success")
                setInterval(function () {
                    location.href = '/login';
                }, 3000);
            } else {
                $('#msg').text(res.message);
                $('#msg').addClass("text-danger");
            }
        },
        error: function (e) {
            alert("unknown error");
        },
    });
}

function associationRegister() {
    var username = $('#inputUsername').val();
    var password = $('#inputPassword').val();
    var nickname = $('#inputNickname').val();
    var identify_name = $("#inputIdentifyName").val();
    var identify_type = $("#inputType").val();
    var description = $("#inputDescription").val();
    var association = {
        username: username,
        password: password,
        nickname: nickname,
        identify_name: identify_name,
        identify_type: identify_type,
        description: description,
    };
    $.ajax({
        type: 'POST',
        contentType: "application/json",
        url: "/register/type=2",
        data: JSON.stringify(association),
        dataType: 'JSON',
        success: function (res) {
            if (res.status) {
                $('#msg').addClass("text-success")
                $('#msg').text(res.message + "将在3s之后跳转到登陆页面");
                setInterval(function () {
                    location.href = '/login';
                }, 3000);
            } else {
                $('#msg').addClass("text-danger");
                $('#msg').text(res.message);
            }
        },
        error: function (e) {
            alert("unknown error");
        },
    });
}

function like(liked_uid) {
    var num = parseInt($("#likenum_max").text());
    var like = {
        liked_uid: liked_uid,
    };
    $.ajax({
        type: "POST",
        url: "/user/like",
        contentType: "application/json",
        data: JSON.stringify(like),
        dataType: "JSON",
        success: function (res) {
            if (res.status) {
                $("#likenum_max").text(num + 1);
                $("#likenum_min").text(num + 1);
                alert("点赞成功");
            } else {
                alert(res.message);
            }
        },
        error: function (e) {
            alert("unknown error");
        },
    });
}



function message(friendname) {
    window.location.href = "/message/send?friendname="+friendname ;
}

